#include <iostream>
#include <cstdlib>
#include <ctime>

int All;
int person = 0;
void class_();
int search();
int class_1[365] = {0};

void class_() // 随机生成生日（0-365）
{
    for (int i = 0; i < person; i++) {
        class_1[i] = rand() % 365+1; // 0 到 365
    }
}

int search() // 检查是否有相同生日
{
    for (int i = 0; i < person; i++) {
        for (int j = i + 1; j < person; j++) {
            if (class_1[i] == class_1[j]) {
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    std::cout << "please enter the total number of the participants \n";
    std::cin >> person;
    std::cout << "please enter the total times of the experiments \n";
    std::cin >> All;
    srand(time(0));
    long double a; // 最终概率
    int all = 0;   // 重复次数计数器
    int i = 0;     // 实验次数计数器

    for (; i < All; i++) {
        class_(); // 生成一组生日
        if (search() == 1) {
            all = all + 1;
        }
        if (i % 100 == 0) {
            std::cout << "the number of the running times is " << i << "\n";
            std::cout << "the situations appear " << all << " times\n";
            // 修复：使用浮点除法计算中间概率
            a = (static_cast<long double>(all) / All) * 100;
            std::cout << "this time the probability is " << a << "%\n";
        }
    }
    a = (static_cast<long double>(all) / All) * 100;
    
    std::cout << "\n\n\n";
    std::cout << "Total experiments: " << All << "\n";
    std::cout << "Participants: " << person 
              << " | Total runs: " << i 
              << " | Matches: " << all 
              << " | Probability: " << a << "%\n";
}
